package com.bjpowernode.config.handler;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.manager.RedisManager;
import com.bjpowernode.result.R;
import com.bjpowernode.util.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 退出成功执行该Handler
 *
 */
@Component
public class DlykLogoutSuccessHandler implements LogoutSuccessHandler {

    @Resource
    private RedisManager redisManager;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //从认证信息中拿到用户对象
        TUser tUser = (TUser)authentication.getPrincipal();

        //删除redis中的token
        Boolean remove = redisManager.removeValue(Constants.REDIS_LOGIN_TOKEN + tUser.getId());

        R result = remove ? R.OK() : R.FAIL();
        ResponseUtil.write(response, result);
    }
}
